﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using BeatEmUp.Characters;
using BeatEmUp.HUD;
using BeatEmUp.Animation;
using BeatEmUp.Characters.Enemies;

namespace BeatEmUp
{
    class Player :Character, IPositionable
    {

        AnimationManager am;
        public Player(Rectangle startingPosition, Texture2D pTextureL, Texture2D pTextureR,GameTime gameTime):
            base(startingPosition, pTextureL, pTextureR, gameTime)
        {
            move = new MovementManager(startingPosition);
            stats.hp = 100;
            stats.maxHp = 100;
            stats.strenght = 1;
            stats.luck = 1;
            stats.level = 1;
            characterHP = new HPDisplay(new Vector2(130, 70), new Vector2(102, 20));

            am = new AnimationManager(new Vector2(30, 51), 4, 4, 500.0f);
        }

        public void UpdatePosition(KeyboardState keyboardState, GamePadState gamePadState)
        {


            (move as MovementManager).DoSomeMovement(keyboardState, gamePadState, gameTime, am);
            UpdateCollisionRec();
        }

        public Rectangle GetPosition()
        {
            return (move as MovementManager).Position;
        }

        public void SetPosition(Rectangle set)
        {
            (move as MovementManager).Position = set;
            UpdateCollisionRec();
        }

        //Walka i obrażenia
        bool isAttacked = false;
        float damageTimeRemaining;
        float damageAnimationTotalTime = 500;

        public int GetLevel()
        {
            return stats.level;
        }

        public Color TextureColor
        {
            get
            {
                if (damageTimeRemaining > 0)
                    return Color.Red;
                return Color.White;
            }
        }

        public void Damage(float enemyDamage)
        {
            isAttacked = true;
            if (stats.hp > 0) stats.hp -= (int)Math.Round(enemyDamage, MidpointRounding.ToEven);//Tutaj będzie to wyliczane ze statystyk przeciwnika
            if (stats.hp < 0 && stats.hp != -1) stats.hp = -1;
            damageTimeRemaining = damageAnimationTotalTime;
        }

        public void DamageGiven(float value)
        {
            stats.damageGiven((int)value);
        }

        public bool IsAttacked
        {
            set { isAttacked = value; }
            get { return isAttacked; }
        }

        public void Update(GameTime gameTime)//Wykorzystywane tylko do zmniejszenia czasu podświetlenia i sprawdzania tego czy jest atakowany
        {
           

            if (isAttacked)
            {
                damageTimeRemaining = MathHelper.Max(0, damageTimeRemaining - (float)gameTime.ElapsedGameTime.TotalMilliseconds);
                if (damageTimeRemaining == 0)
                {
                    isAttacked = false;
                    if (stats.hp == -1) stats.hp = 0;
                    
                }
            }
        }

        public void Draw()
        {
            GameManager.customResolution.Draw(CharacterTexture, GetPosition(),am.GetRectangle(),TextureColor);
        }

        internal Stats getStats()
        {
            return this.stats;
        }

        internal void setStats(Stats newStats)
        {
            this.stats = newStats;
        }

        public int CurrentFrame
        {
            get
            {
                return am.CurrentFrame;
            }
        }
        internal void checkEnemyPosition(Rectangle enemyCollision)
        {

            if (collisionRec.Intersects(enemyCollision))
            {

                if (CollisionRec.X < enemyCollision.X)
                {
                    (move as EnemiesMovementManager).getCloser(MoveEnum.Left);
                    (move as EnemiesMovementManager).FaceDirection = FaceDirectionEnum.Left;
                }
                if (CollisionRec.X > enemyCollision.X)
                {
                    (move as EnemiesMovementManager).getCloser(MoveEnum.Right);
                    (move as EnemiesMovementManager).FaceDirection = FaceDirectionEnum.Left;
                }
                if (CollisionRec.Y < enemyCollision.Y)
                {
                    (move as EnemiesMovementManager).getCloser(MoveEnum.Up);
                }
                if (CollisionRec.Y > enemyCollision.Y)
                {
                    (move as EnemiesMovementManager).getCloser(MoveEnum.Down);
                }

                UpdateCollisionRec();
            }
        }
    }
}
